import os
import PyPDF2


def pdf_catalog(input_file, output_file, start_page, end_page):

    if not os.path.exists(input_file) or os.path.exists(output_file) or input_file == output_file:
        return

    pdf_input = PyPDF2.PdfFileReader(open(input_file, "rb"))
    page_num = pdf_input.getNumPages()

    if start_page > end_page or start_page < 1 or end_page > page_num:
        return

    pdf_output = PyPDF2.PdfFileWriter()

    i = start_page - 1
    while i < end_page:
        page = pdf_input.getPage(i)
        pdf_output.addPage(page)
        i = i + 1

    pdf_output.write(open(output_file, "wb"))


def pdf_split(pdf_file, delta, output_dir):

    if not os.path.exists(pdf_file):
        return

    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    pdf_input = PyPDF2.PdfFileReader(open(pdf_file, "rb"))
    page_num = pdf_input.getNumPages()

    pass


if __name__ == '__main__':
    in_fn = 'E:\下载\语音信号处理 赵力.pdf'
    out_fn = 'E:\下载\语音信号处理 赵力 目录.pdf'
    start_num = 8
    end_num = 11

    pdf_catalog(in_fn, out_fn, start_num, end_num)